﻿using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Heima.OA.BLL
{
    public partial class ZS_CheckInfoService : BaseService<ZS_CheckInfo>, IZS_CheckInfoService
    {
        /// <summary>
        /// 通过申报家庭成员 查找核查房屋信息
        /// </summary>
        /// <param name="DeclareFamilyId"></param>
        /// <returns></returns>
        public List<ZS_CheckHouseInfo> GetCheckHouseList(int DeclareFamilyId)
        {
            List<ZS_CheckHouseInfo> checkHouseList = new List<ZS_CheckHouseInfo>();
            var memberList = DbSession.ZS_MemberDal.GetEntities(u => u.DelFlag == 0 && u.FamilyId == DeclareFamilyId);
            foreach(var item in memberList)
            {
                var tempHouseList = GetCheckHouseListByMemberId(item.Id);
                tempHouseList.ForEach(u =>
                {
                    if (!checkHouseList.Exists(x => x.Id == u.Id))
                    {
                        checkHouseList.Add(u);
                    }
                });
            }

            return checkHouseList;
        }
        /// <summary>
        /// 查找当事人 申报的房屋信息
        /// </summary>
        /// <param name="MemberId"></param>
        /// <param name="msg"></param>
        public List<ZS_CheckHouseInfo> GetCheckHouseListByMemberId(int MemberId)
        {
            List<ZS_CheckHouseInfo> checkHouseList = new List<ZS_CheckHouseInfo>();
            ZS_DeclareInfo declareInfo = DbSession.ZS_DeclareInfoDal.GetEntities(u => u.DelFlag == 0 && u.MemberId == MemberId).FirstOrDefault();
            if(declareInfo != null)
            {
                var houseList = DbSession.ZS_HouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.DeclareId == declareInfo.Id).ToList();
                //通过申报房屋 查找核查房屋
                foreach (var item in houseList)
                {
                    ZS_CheckHouseInfo checkHouse = DbSession.ZS_CheckHouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.DeclareHouseId == item.Id).First();
                    if (checkHouse != null)
                    {
                        checkHouseList.Add(checkHouse);
                    }
                }
            }
            return checkHouseList;
        }
        /// <summary>
        /// 添加或编辑核查房屋信息
        /// </summary>
        /// <param name="checkId"></param>
        /// <param name="buildingId"></param>
        /// <param name="checkHouseList"></param>
        /// <returns></returns>
        public bool AddCheckHouse(int checkId, int buildingId, List<ZS_CheckHouseInfo> checkHouseList)
        {
            foreach(var item in checkHouseList)
            {
                if (item.Id > 0)
                {
                    ZS_CheckHouseInfo checkHouse = DbSession.ZS_CheckHouseInfoDal.GetModel(item.Id);
                    checkHouse.CheckBuildingId = buildingId;
                    checkHouse.HouseName = item.HouseName;
                    checkHouse.HouseArea = item.HouseArea;
                    checkHouse.DecorationSituation = item.DecorationSituation;
                    checkHouse.ActualityUse = item.ActualityUse;
                    checkHouse.Remark = item.Remark;
                    checkHouse.CheckId = checkId;
                    checkHouse.CerName = item.CerName;
                    checkHouse.CerCode = item.CerCode;
                    checkHouse.RegisterRight = item.RegisterRight;
                    checkHouse.RegisterBuildingArea = item.RegisterBuildingArea;
                    checkHouse.ContractSigner = item.ContractSigner;
                    checkHouse.ContractRegisterBuildingArea = item.ContractRegisterBuildingArea;
                    checkHouse.BuildingCode = item.BuildingCode;
                    checkHouse.ViolationOriginCode = item.ViolationOriginCode;
                    checkHouse.IsBuiltBeforeDate = item.IsBuiltBeforeDate;
                    checkHouse.BuiltAreaBeforeDate = item.BuiltAreaBeforeDate;
                    checkHouse.BuiltAreaAfterDate = item.BuiltAreaAfterDate;
                    checkHouse.ViolationBuiltArea = item.ViolationBuiltArea;
                    DbSession.ZS_CheckHouseInfoDal.Update(checkHouse);
                }
                else
                {
                    item.CheckBuildingId = buildingId;
                    item.CheckId = checkId;
                    item.DelFlag = 0;
                    item.CreateTime = DateTime.Now;
                    DbSession.ZS_CheckHouseInfoDal.Add(item);
                }
            }
            return DbSession.SaveChanges() > 0;
        }
        /// <summary>
        /// 添加或编辑擅改商信息
        /// </summary>
        /// <param name="checkId"></param>
        /// <param name="buildingId"></param>
        /// <param name="checkBusinessList"></param>
        /// <returns></returns>
        public bool AddPrivateBusiness(int checkId, int buildingId, List<ZS_CheckPrivatelyBusinessInfo> checkBusinessList)
        {
            foreach(var item in checkBusinessList)
            {
                if (item.Id > 0)
                {
                    ZS_CheckPrivatelyBusinessInfo businessInfo = DbSession.ZS_CheckPrivatelyBusinessInfoDal.GetModel(item.Id);
                    businessInfo.BuildingCode = item.BuildingCode;
                    businessInfo.StoreName = item.StoreName;
                    businessInfo.CerNameCode = item.CerNameCode;
                    businessInfo.OperatorName = item.OperatorName;
                    businessInfo.OperatorProveFileName = item.OperatorProveFileName;
                    businessInfo.ActualMonths = item.ActualMonths;
                    businessInfo.PrivateArea = item.PrivateArea;
                    businessInfo.OnFloor = item.OnFloor;
                    businessInfo.BuildingId = buildingId;
                    businessInfo.CheckId = checkId;
                    DbSession.ZS_CheckPrivatelyBusinessInfoDal.Update(businessInfo);
                }
                else
                {
                    item.BuildingId = buildingId;
                    item.CheckId = checkId;
                    item.DelFlag = 0;
                    item.CreateTime = DateTime.Now;
                    DbSession.ZS_CheckPrivatelyBusinessInfoDal.Add(item);
                }
            }
            return DbSession.SaveChanges() > 0;
        }
        /// <summary>
        /// 添加或编辑已安置房屋信息
        /// </summary>
        /// <param name="checkId"></param>
        /// <param name="familyId"></param>
        /// <param name="checkResettlementList"></param>
        /// <returns></returns>
        public bool AddResettlement(int checkId, int familyId, List<ZS_CheckResettlementInfo> checkResettlementList)
        {
            foreach(var item in checkResettlementList)
            {
                if (item.Id > 0)
                {
                    ZS_CheckResettlementInfo resettlementInfo = DbSession.ZS_CheckResettlementInfoDal.GetModel(item.Id);
                    resettlementInfo.MemberNames = item.MemberNames;
                    resettlementInfo.ItemName = item.ItemName;
                    resettlementInfo.RightCerName = item.RightCerName;
                    resettlementInfo.CerCode = item.CerCode;
                    resettlementInfo.RegisterBuildingArea = item.RegisterBuildingArea;
                    resettlementInfo.AllocateArea = item.AllocateArea;
                    resettlementInfo.ReturnHouseCode = item.ReturnHouseCode;
                    resettlementInfo.ReturnHouseArea = item.ReturnHouseArea;
                    resettlementInfo.OtherItemName = item.OtherItemName;
                    resettlementInfo.OtherAllocateArea = item.OtherAllocateArea;
                    resettlementInfo.MoneyCompensateArea = item.MoneyCompensateArea;
                    resettlementInfo.SumArea = item.SumArea;
                    resettlementInfo.FamilyId = familyId;
                    resettlementInfo.CheckId = checkId;
                    DbSession.ZS_CheckResettlementInfoDal.Update(resettlementInfo);
                }
                else
                {
                    item.FamilyId = familyId;
                    item.CheckId = checkId;
                    item.DelFlag = 0;
                    item.CreateTime = DateTime.Now;
                    DbSession.ZS_CheckResettlementInfoDal.Add(item);
                }
            }
            return DbSession.SaveChanges()>0;
        }
        /// <summary>
        /// 获取编辑数据
        /// </summary>
        /// <param name="checkModel"></param>
        /// <returns></returns>
        public CheckInfoView GetEditViewModel(ZS_CheckInfo checkModel)
        {
            var checkHouseList = DbSession.ZS_CheckHouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.CheckId == checkModel.Id).ToList();
            var checkBusinessList = DbSession.ZS_CheckPrivatelyBusinessInfoDal.GetEntities(u=> u.DelFlag == 0 && u.CheckId == checkModel.Id).ToList();
            var checkResettlementList = DbSession.ZS_CheckResettlementInfoDal.GetEntities(u => u.DelFlag == 0 && u.CheckId == checkModel.Id).ToList();
            var checkFamily = DbSession.ZS_CheckFamilyInfoDal.GetModel(checkModel.FamilyId ?? 0);
            CheckInfoView view = new CheckInfoView
            {
                Id = checkModel.Id,
                DelFlag = checkModel.DelFlag,
                CreateTime = checkModel.CreateTime,
                FamilyId = checkModel.FamilyId,
                DeclareFamilyId = checkFamily!=null ? checkFamily.DeclareFamilyId??0 : 0,
                BuildingId = checkModel.BuildingId,
                CheckHouseList = checkHouseList,
                BusinessList = checkBusinessList,
                ResettlementList = checkResettlementList
            };
            return view;
        }
        public List<object> GetIndexList(List<ZS_CheckInfo> checkList)
        {
            List<object> objList = new List<object>();
            foreach(var item in checkList)
            {
                ZS_CheckFamilyInfo checkFamily = DbSession.ZS_CheckFamilyInfoDal.GetModel(item.FamilyId ?? 0);
                ZS_CheckBuildingInfo checkBuilding = DbSession.ZS_CheckBuildingInfoDal.GetModel(item.BuildingId ?? 0);
                var houseList = DbSession.ZS_CheckHouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.CheckId == item.Id).ToList();
                string houseNames = string.Empty;
                houseList.ForEach(u => houseNames += u.HouseName + ",");
                houseNames = !string.IsNullOrEmpty(houseNames) ? houseNames.TrimEnd(',') : "";
                object obj = new
                {
                    item.Id,
                    item.JingbanPerson,
                    item.CheckPerson,
                    item.CheckDate,
                    item.DelFlag,
                    item.FamilyId,
                    item.BuildingId,
                    PowerMemberName = checkFamily != null ? checkFamily.PowerMemberName : "",
                    RegisterNumber = checkFamily != null ? checkFamily.RegisterNumber.ToString() : "",
                    BuildingName = checkBuilding != null ? checkBuilding.BuildingName : "",
                    HouseNames = houseNames
                };
                objList.Add(obj);
            }
            return objList;
        }
        /// <summary>
        /// 删除政策性住房
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteResettlement(int id)
        {
            ZS_CheckResettlementInfo resettlement = DbSession.ZS_CheckResettlementInfoDal.GetModel(id);
            if (resettlement != null)
            {
                resettlement.DelFlag = (short)DelFlagEnum.Deleted;
                DbSession.ZS_CheckResettlementInfoDal.Update(resettlement);
                return DbSession.SaveChanges()>0;
            }
            else
            {
                return false;
            }
        }
        public bool DeleteBusiness(int id)
        {
            ZS_CheckPrivatelyBusinessInfo business = DbSession.ZS_CheckPrivatelyBusinessInfoDal.GetModel(id);
            if (business != null)
            {
                business.DelFlag= (short)DelFlagEnum.Deleted;
                DbSession.ZS_CheckPrivatelyBusinessInfoDal.Update(business);
                return DbSession.SaveChanges()>0;
            }
            else
            {
                return false;
            }
        }
        public object BuildingCheckInfo(int id)
        {
            ZS_CheckBuildingInfo checkBuilding = DbSession.ZS_CheckBuildingInfoDal.GetModel(id);
            if(checkBuilding != null)
            {
                var checkList = DbSession.ZS_CheckInfoDal.GetEntities(u => u.DelFlag == 0 && u.BuildingId == checkBuilding.Id).ToList();
                //核查表个数
                int checkCount = checkList.Count;
                //已核查面积
                decimal totalHouseArea = 0;
                foreach(var check in checkList)
                {
                    var checkHouseList = DbSession.ZS_CheckHouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.CheckId == check.Id).ToList();
                    totalHouseArea += checkHouseList.Sum(u => u.HouseArea) ?? 0;
                }
                var outHouseList = DbSession.ZS_CheckHouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.CheckBuildingId == checkBuilding.Id).ToList();
                object obj = new
                {
                    checkBuilding,
                    checkCount,
                    totalHouseArea,
                    outHouseList
                };
                return obj;
            }
            else
            {
                return null;
            }
        }


    }
}
